package edu.nf.hotel.regiondao.impl;

import edu.nf.hotel.region.HotelInformation;
import edu.nf.hotel.region.all.CityHotel;
import edu.nf.hotel.regiondao.HotelInformationDao;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.MapHandler;

import java.util.List;
import java.util.Map;

/**
 * @Date 2023-06-19
 * @Author 带土
 */
public class HotelInformationDaoImpl implements HotelInformationDao {
    /**
     * 实现添加酒店信息
     * @param hotelInformation
     */
    @Override
    public void getAddHotelInformation(HotelInformation hotelInformation) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "insert into hotel_information(c_id,hotel_name,hotel_location,hotel_notes) values (?,?,?,?)";
        executor.executeUpdate(sql,hotelInformation.getCid(),hotelInformation.getHotelName(),hotelInformation.getHotelLocation(),hotelInformation.getHotelNotes());
    }

    /**
     * 实现删除酒店信息
     * @param hotel_id
     */
    @Override
    public void getDeleteHotelInformation(int hotel_id) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "delete from hotel_information where hotel_id = ?";
        executor.executeUpdate(sql,hotel_id);
    }

    /**
     * 实现修改酒店信息
     * @param h
     */
    @Override
    public void getUpdateHotelInformation(HotelInformation h) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "update hotel_information set c_id=?,hotel_name=?,hotel_location=?,hotel_notes=? where hotel_id = ? ";
        executor.executeUpdate(sql,h.getCid(),h.getHotelName(),h.getHotelLocation(),h.getHotelNotes(),h.getHotelId());

    }

    /**
     * 根据地区名称查询全部
     * @param cityName
     * @return
     */
    @Override
    public List<CityHotel> getAllName(String cityName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select city_id,city_name,city_code,city_scape_image,hotel_id,hotel_name,hotel_location,hotel_notes" +
                " from city_type inner join hotel_information hi on city_type.city_id = hi.c_id where city_name=?" +
                " group by city_id,city_name,city_code,city_scape_image,hotel_id,hotel_name,hotel_location,hotel_notes";
        BeanListHandler<CityHotel> handler = new BeanListHandler<>(CityHotel.class);
        List<CityHotel> list = executor.executeQuery(sql, handler, cityName);
        System.out.println(list);
        return list;
    }

    /**
     * 实现查询全部酒店信息
     * @return
     */
    @Override
    public List<HotelInformation> getAllHotelInformation() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select hotel_id,c_id,hotel_name,hotel_location,hotel_notes from hotel_information";
        BeanListHandler<HotelInformation> handler = new BeanListHandler<>(HotelInformation.class);
        List<HotelInformation> list = executor.executeQuery(sql, handler);
        list.forEach(hotelInformation -> System.out.println(hotelInformation.getHotelName()));
        return list;
    }

    /**
     * 对字段进行分字段查询
     * @param hotelName
     * @return
     */
    @Override
    public Map<String, Object> getHotelDescription(String hotelName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select " +
                "substring_index(hotel_notes, '.', 1)," +
                "substring_index(substring_index(hotel_notes, '.', 2), '.', -1), " +
                "substring_index(substring_index(hotel_notes, '.', 3), '.', -1), " +
                "substring_index(hotel_notes, '.', -1) " +
                "from hotel_information where hotel_name=?";
        MapHandler handler = new MapHandler();
        Map<String, Object> maps = executor.executeQuery(sql, handler,hotelName);
        System.out.println(maps);
        return maps;
    }

}
